import api from '../../api';
import { message } from 'antd';
export default {
  namespace: 'ApprovalUtilsM',

  state: {
    poolNameData: [],
    selectData: {},
    currentUser: {},
    detailData: {},
    tagData: {},
    issuer: [],
    bringTagData: {},
    researchers: [],
  },

  effects: {
    //
    *searchContent({ payload, callback }, { call, put }) {
      const res = yield call(api.poolApproval.searchContent, payload);
      if (res && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            issuer: res.data.dataSource,
          },
        });
      } else {
        message.error('获取内容失败');
      }
    },

    // 获取券池名称
    *getPoolName({ payload, callback }, { call, put }) {
      const res = yield call(api.poolApproval.getPoolName, payload);
      if (res) {
        yield put({
          type: 'save',
          payload: {
            poolNameData: res,
          },
        });
      } else {
        message.error('获取券池名称失败');
      }
    },

    // 获取债券信息
    *getSecuritiesDetail({ payload, callback }, { call, put }) {
      const query = {
        tradingCode: payload.tradingCode,
        exchangeCode: payload.exchangeCode,
      };
      const res = yield call(api.getSecuritiesDetail[payload.type], query);
      if (res && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            threeDatas: res.data.dataSource,
          },
        });
        callback(res.data.dataSource);
      } else {
        message.error('获取债券信息失败');
      }
    },

    // 出池带出标签值
    *bringTag({ payload, callback }, { call, put }) {
      const res = yield call(api.poolApproval.bringTag, payload);
      if (res && res.code == 0) {
        callback(res.data.dataSource);
      } else {
        message.error(res.msg);
      }
    },

    // 查询详细信息
    *getDetails({ payload, callback }, { call, put }) {
      const res = yield call(api.poolApproval.getDetails, payload);
      if (res && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            detailData: res.data.dataSource,
          },
        });
        callback(res.data);
      } else {
        message.error(res.msg || '查询列表失败');
      }
    },
    //查询所有
    *findAll({ payload }, { call, put }) {
      const res = yield call(api.poolApproval.findAll, payload);
      if (res && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            showData: res.data,
          },
        });
      } else {
        message.error('查询列表失败');
      }
    },

    // 获取标签
    *findTag({ payload }, { call, put }) {
      const res = yield call(api.poolApproval.findTag, payload);
      if (res) {
        yield put({
          type: 'save',
          payload: {
            tagData: res,
          },
        });
      }
    },
    // 获取研究员
    *findResearcher({ payload }, { call, put }) {
      const res = yield call(api.poolApproval.findResearcher, payload);
      if (res) {
        yield put({
          type: 'save',
          payload: {
            researchers: res,
          },
        });
      }
    },
    //获取用户信息
    *getUserInfo({ payload, callback }, { call, put }) {
      const response = yield call(api.user.queryCurrent, payload);
      if (response) {
        yield put({
          type: 'save',
          payload: {
            currentUser: response.data,
          },
        });
        callback(response.data);
      }
    },
  },
  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
